package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;

import cn.com.cennavi.bean.DifPointTotalMapInfo;
import cn.com.cennavi.util.MapUtil;

/**
 * 根据标准答案经过的缓冲区，去筛选地图路口数据
 * 
 * @author Thinkpad
 *
 */
public class MapDataFilter {
	static String mapFile = "D:\\data\\DifPointTotalMapInfo_closeRoad_new_quan.csv";
	static String standardFile = "D:/data/standard_20100827.log";
	static String outFilterFile = "D:/data/滴滴分车道路况标准答案/201606/20160630经过的缓冲区地图数据.txt";

	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub
		HashMap<String, ArrayList<String>> mapDataMap = new HashMap<String, ArrayList<String>>();
		mapDataMap = getMapDataMap(mapFile);
		HashMap<String, Geometry> mapfilter = mapfilter(mapDataMap, standardFile);
		outMapToFile(mapfilter, mapFile);
	}

	private static void outMapToFile(HashMap<String, Geometry> mapfilter, String mapFile) throws IOException, ParseException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(mapFile)));
		String line = "";
		while ((line = br.readLine()) != null) {
			DifPointTotalMapInfo difPointTotalMapInfo = DifPointTotalMapInfo.parseData1(line);
			if (mapfilter.containsKey(difPointTotalMapInfo.roadId)) {
				System.out.println(line);
			}
		}
	}

	private static HashMap<String, Geometry> mapfilter(HashMap<String, ArrayList<String>> mapDataMap, String standardFile2) throws IOException {
		// TODO Auto-generated method stub
		HashMap<String, Geometry> getMapDataMap = new HashMap<String, Geometry>();
		BufferedReader br = new BufferedReader(new FileReader(new File(standardFile2)));
		String line = "";
		HashSet<String> difpoint = new HashSet<String>();
		while ((line = br.readLine()) != null) {
			String[] arr = line.split(",");
			String id = arr[0];
			difpoint.add(id);
			// double lon = Double.parseDouble(arr[4]);
			// double lat = Double.parseDouble(arr[3]);
			// Point point = MapUtil.getPoint(new Coordinate(lon, lat));
			// for (Map.Entry<String, Geometry> entry : mapDataMap.entrySet()) {
			// String roadId = entry.getKey();
			// Geometry geo = entry.getValue();
			// if (geo.contains(point)) {
			// getMapDataMap.put(roadId, geo);
			// }
			// }
		}
		for (String id : difpoint) {
			if (mapDataMap.containsKey(id)) {
				ArrayList<String> list = mapDataMap.get(id);
				for (String s : list) {
					System.out.println(s);
				}
			}
		}
		return getMapDataMap;
	}

	private static HashMap<String, ArrayList<String>> getMapDataMap(String mapFile2) throws IOException, ParseException {
		// TODO Auto-generated method stub
		HashMap<String, ArrayList<String>> getMapDataMap = new HashMap<String, ArrayList<String>>();
		BufferedReader br = new BufferedReader(new FileReader(new File(mapFile)));
		String line = "";
		while ((line = br.readLine()) != null) {
			DifPointTotalMapInfo difPointTotalMapInfo = DifPointTotalMapInfo.parseData1(line);
			String dif_Point_Id = difPointTotalMapInfo.dif_Point_Id;
			// Geometry geo = difPointTotalMapInfo.roadGeo;
			ArrayList<String> dd = getMapDataMap.get(dif_Point_Id);
			if (dd == null) {
				dd = new ArrayList<String>();
				dd.add(line);
				getMapDataMap.put(dif_Point_Id, dd);
			} else {
				dd.add(line);
			}
		}
		return getMapDataMap;
	}
}
